package com.yangqi.io;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;

public class CreateDir {
    public static void main(String[] args) {
        String path = "D:\\home\\cache";
        File file = new File(path);
       if(file.exists() && file.isDirectory()){
           System.out.println("文件已经存在");
       }else{
           if(file.mkdirs()){
               System.out.println("文件创建成功");
           }else{
               System.out.println("文件创建失败");
           }
       }
       String path1 = "http://www.lanou3g.com/ueditor/php/upload/image/20170630/1498786965170810.jpg";
       String path2 = "D:\\home\\cache\\copy.jpg";
        BufferedOutputStream bos = null;
        URL url = null;
        try {
            url = new URL(path1);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        try {
            URLConnection conn = url.openConnection();
            InputStream inStream = conn.getInputStream();
//            bis = new BufferedInputStream(new FileInputStream(path1));
            bos = new BufferedOutputStream(new FileOutputStream(path2));
            byte[] bytes = new byte[1024];
            int readLength = 0;
            while((readLength = inStream.read(bytes)) != -1){
                bos.write(bytes,0,readLength);
            }
            System.out.println("下载成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
           try{
               if (bos != null){
                   bos.close();
               }
           }catch (IOException e){
               throw new RuntimeException(e);
           }

        }


    }

}
